(function() {

function traditionalCancel (e) {
  ok(true, 'Invoked traditionalCancel function');
  return false;
}

function modernCancel (e) {
  ok(true, 'Invoked modernCancel function');
  if (e.preventDefault) {
    e.preventDefault();
  } else {
    e.returnValue = false;
  }
}

function addEvent (node, type, fn) {
  if (node.addEventListener) {
    node.addEventListener(type, fn, false);
  } else if (node.attachEvent) {
    node.attachEvent('on' + type, fn);
  }
}

function typicalClick (e) {
  ok(true, 'Invoked typicalClick function');
  equals(e.type, 'click', 'Event type is click');
}

module('simE');

test('Errors', function() {
  try {
    simE();
    ok(false, "Shouldn't come here");
  } catch(e) {
    equals(e.message, 'Not a valid DOM node', 'Throws error if called w/o a DOM node.');
  }

  try {
    simE(document);
    ok(false, "Shouldn't come here");
  } catch (e) {
    equals(e.message, 'Must specify an event type', 'Throws error if no event type is present');
  }
  
  var fakeNode = {nodeType: 1};
  try {
    simE(fakeNode, 'click');
    ok(false, "Shouldn't come here");
  } catch (e) {
    equals(e.message, 'Your browser is way too old.', 'Throws error if node cannot fire event');
  }
});

test('Traditional event handler', function() {
  expect(6);

  var div = document.createElement('div'), notCancelled;
  div.onclick = traditionalCancel;

  notCancelled = simE(div, 'click');
  ok(notCancelled === false, '"return false" works.');

  div.onclick = modernCancel;
  notCancelled = simE(div, 'click');
  ok(notCancelled === false, 'preventDefault or assign returnValue works.');

  div.onclick = typicalClick;
  simE(div, 'click');
});

test('Modern event handler', function() {
  expect(2);
  var span = document.createElement('span');
  addEvent(span, 'click', typicalClick);
  simE(span, 'click');
});

})();

